//获取应用实例
const app = getApp()
const baseUrl = 'http://jbc.com';
Page({
 
    /**
     * 页面的初始数据
     */
    data: { 
      tihuoWay:'经销商',
      pid:1,
      select: false, 
      role_type:1,
      name:'',
      phone:'',
      identify:'',
      identify_picp:'',
      identify_picb:'',
      business_pic:'', 
      province: '',
      city: '',
      district: '',
      detail:'',
      join_money:'',
      userInfo:[],
      provinces: '',
      citys: '',
      districts: '',
      provinceIndex: 0,
      cityIndex: 0,
      districtIndex: 0,
      provinceCode: '',
      cityCode: '',
      districtCode: '',
      
    },
 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
      var that = this;
      wx.getStorage({//获取本地缓存
        key: "user",
        success: function (res) {
          that.setData({
            userInfo: res.data,
          }); 
        }
      });  
      var that = this
      var header = { 'content-type': 'application/json' }
      wx.request({
        url: baseUrl+'/system/User/getProvinceList', //查询省信息parentId为空
        method: 'POST',
        header: header,
        success: function (res) {
          var code = res.data.code;
          var datas = res.data.data;
          if (code > 0) {
            var provinceArray = []
            var provinceCodeArray = []
            var cityParentId = datas[0].rowid //省编码，用于默认级联查询
            for (var iprovince = 0; iprovince < datas.length; iprovince++) {
              provinceArray[iprovince] = datas[iprovince].areaname
              provinceCodeArray[iprovince] = datas[iprovince].rowid
            }
            that.setData({
              province: datas[0].areaname,
              provinces: provinceArray,
              provinceCode: provinceCodeArray,
            })
            wx.request({
              url: baseUrl +'/system/User/getCityList',
              method: 'POST',
              header: header,
              data: { rowid: cityParentId },
              success: function (cityRes) {
                var cityRespCode = cityRes.data.code;
                var city = cityRes.data.data;
                if (code > 0) {
                  var cityArray = []
                  var cityCodeArray = []
                  var districtParentId = city[0].rowid //市编码，用于默认级联查询 
                  for (var icity = 0; icity < city.length; icity++) {
                    cityArray[icity] = city[icity].areaname
                    cityCodeArray[icity] = city[icity].rowid
                  }
                  that.setData({
                    city: city[0].areaname,
                    citys: cityArray,
                    cityCode: cityCodeArray
                  })
                  wx.request({
                    url: baseUrl +'/system/User/getCityList',
                    method: 'POST',
                    header: header,
                    data: { rowid: districtParentId },
                    success: function (districtRes) {
                      var districtRespCode = districtRes.data.code;
                      var district = districtRes.data.data;
                      if (districtRespCode > 0 && district.length > 0) {
                        var districtArray = []
                        var districtCodeArray = []
                        var streetParentId = district[0].rowid //区编码 用于默认级联查询 
                        for (var idistrict = 0; idistrict < district.length; idistrict++) {
                          districtArray[idistrict] = district[idistrict].areaname
                          districtCodeArray[idistrict] = district[idistrict].rowid
                        }
                        that.setData({
                          district: district[0].areaname,
                          districts: districtArray,
                          districtCode: districtCodeArray
                        })
                      } else {
                        wx.showToast({
                          title: '数据加载!',
                          icon: 'loading',
                          duration: 1500,
                        })
                      }
                    }
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: '数据加载!',
              icon: 'loading',
              duration: 1500,
            })
          }
        }
      })
    },
    bindShowMsg() {
      this.setData({
        select: !this.data.select
      })
    },
    mySelect(e) {
      var name = e.currentTarget.dataset.name;
      var types = e.currentTarget.dataset.types
      this.setData({
        tihuoWay: name,
        role_type: types,
        select: false
      })
    },

    onUploadp:function(e){
      //上传图片
      var that = this;
      var ids = "identify_picp";
      wx.chooseImage({
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;
         
          var path = that.uploadImage(tempFilePaths, ids);  
        } 
     }) 
    },
    onUploadb: function (e) {
      //上传图片
      var that = this;
      var ids = "identify_picb";
      wx.chooseImage({
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;
          var path = that.uploadImage(tempFilePaths, ids);
        }
      })
    },
    onUploadBuss: function (e) {
      //上传图片
      var that = this;
      var ids = "business_pic";
      wx.chooseImage({
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;
          var path = that.uploadImage(tempFilePaths, ids);
        }
      })
    },

  
    uploadImage: function (tempFilePaths, ids){  
      var pic = ""; 
      var that = this;
          wx.uploadFile({
            url: baseUrl +'/system/User/ajaxuploads', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'file', 
            header: {
              'Content-Type': 'application/json' 
            },
            success: function (res) {
              console.log(res); 
              var datas = JSON.parse(res.data); 
              if (datas.status == 1) {
                pic = datas.path; 
                if (ids == 'identify_picp'){
                  that.setData({
                    identify_picp: baseUrl+pic,
                  }); 
                } else if (ids == 'identify_picb'){
                  that.setData({
                    identify_picb: baseUrl + pic,
                  }); 
                } else if (ids == 'business_pic') {
                  that.setData({
                    business_pic: baseUrl + pic,
                  });
                }
               
              } else {
                wx.showToast({
                  title: '上传失败',
                  icon: 'loading',
                  duration: 1500 
                }) 
              }   
              return pic;
            }
          }) ;  
    }, 

  onShowToast(title){
    wx.showToast({
      title: title,
      icon: 'loading',
      duration: 1500
    })
    setTimeout(function () {
      wx.hideToast()
    }, 2000)

  },

// 提交表单
    formSubmit: function (e) {
      var that = this; 
      if (e.detail.value.name.length == 0) {
        that.onShowToast("姓名不得为空");

      } else if (e.detail.value.phone.length == 0) {
          that.onShowToast("电话号码不得为空");
      
      } else if (e.detail.value.phone.length != 11) {

        wx.showToast({

          title: '请输入11位手机号码!',

          icon: 'loading',

          duration: 1500

        })

        setTimeout(function () {

          wx.hideToast()

        }, 2000)

      }else {

        wx.request({

          url: baseUrl  +"/System/User/edit_member",

          header: {

            // "Content-Type": "application/x-www-form-urlencoded"
             'Content-Type': 'application/json'

          },

          method: "POST",

          data: { 
          member_id: that.data.userInfo.id,
          role_type: that.data.role_type,
          name: e.detail.value.name, 
          phone: e.detail.value.phone ,
          identify_picp: e.detail.value.identify_picp,
          identify_picb: e.detail.value.identify_picb,
          identify: e.detail.value.identify,
          provinces: that.data.provinces,
          city: that.data.city,
          district: that.data.district,
          business_card: e.detail.value.business_card,
          pid: e.detail.value.pid, 
          },

          success: function (res) { 
            if (res.data.status == 0) {

              wx.showToast({

                title: res.data.msg,

                icon: 'loading',

                duration: 1500

              })

            } else {

              wx.showToast({

                title: res.data.msg,//这里打印出登录成功

                icon: 'success',

                duration: 1000

              })

            }

          }
        })
      }
    },


   //省份选择
    provincePicker: function (e) {
      var that = this;
      var header = { 'content-type': 'application/json' }
      //改变index值，通过setData()方法重绘界面
      var currentProIndex = e.detail.value
      var currentProCode = e.currentTarget.dataset.procode[currentProIndex] //当前省代表的Code,当做查询级联市的parentid
      var province = e.currentTarget.dataset.name[currentProIndex];
  
      that.setData({
        province: province,
        provinceIndex: currentProIndex,
        cityIndex: 0,
        districtIndex: 0,
      })
      wx.request({
        url: baseUrl+'/system/User/getCityList',
        method: 'POST',
        header: header,
        data: { rowid: currentProCode },
        success: function (cityRes) {
          var cityRespCode = cityRes.data.code;
          var city = cityRes.data.data;
          if (cityRespCode > 0) {
            var cityArray = []
            var cityCodeArray = []
            var districtParentId = city[0].rowid //市编码，用于默认级联查询 
            for (var icity = 0; icity < city.length; icity++) {
              cityArray[icity] = city[icity].areaname
              cityCodeArray[icity] = city[icity].rowid
            }
            that.setData({
              city: city[0].areaname,
              citys: cityArray,
              cityCode: cityCodeArray
            })

            wx.request({
              url: baseUrl+'/system/User/getCityList',
              method: 'POST',
              header: header,
              data: { rowid: districtParentId },
              success: function (districtRes) {
                var districtRespCode = districtRes.data.code;
                var district = districtRes.data.data;
                if (districtRespCode > 0 && district.length > 0) {
                  var districtArray = []
                  var districtCodeArray = []
                  var streetParentId = district[0].rowid //区编码 用于默认级联查询 
                  for (var idistrict = 0; idistrict < district.length; idistrict++) {
                    districtArray[idistrict] = district[idistrict].areaname
                    districtCodeArray[idistrict] = district[idistrict].rowid
                  }
                  that.setData({ 
                    district: district[0].areaname,
                    districts: districtArray,
                    districtCode: districtCodeArray
                  })
                } else {
                  wx.showToast({
                    title: '数据加载!',
                    icon: 'loading',
                    duration: 1500,
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: '数据加载!',
              icon: 'loading',
              duration: 1500,
            })
          }
        }
      })
    },
    //选择城市
    cityPicker: function (e) { 
      var that = this
      var header = { 'content-type': 'application/json' }
      //改变index值，通过setData()方法重绘界面
      var currentCityIndex = e.detail.value 
      var currentCityCode = e.currentTarget.dataset.citycode[currentCityIndex]; //当前市代表的Code,当做查询级联区的parentid
      var city = e.currentTarget.dataset.name[currentCityIndex]; //当前市代表的Code,当做查询级联区的parentid
   
      that.setData({
        city: city,
        cityIndex: currentCityIndex,
        districtIndex: 0,
      })
      wx.request({
        url: baseUrl+'/system/User/getCityList',
        method: 'POST',
        header: header,
        data: { rowid: currentCityCode },
        success: function (districtRes) {
          var districtRespCode = districtRes.data.code;
          var district = districtRes.data.data;
          if (districtRespCode > 0 && district.length > 0) {
            var districtArray = []
            var districtCodeArray = []
            var streetParentId = district[0].rowid //区编码 用于默认级联查询 
            for (var idistrict = 0; idistrict < district.length; idistrict++) {
              districtArray[idistrict] = district[idistrict].areaname
              districtCodeArray[idistrict] = district[idistrict].rowid
            }
            that.setData({
              district: district[0].areaname,
              districts: districtArray,
              districtCode: districtCodeArray
            })
          } else {
            wx.showToast({
              title: '数据加载!',
              icon: 'loading',
              duration: 1500,
            })
          }
        }
      })
    },
    //街道选择
    districtPicker: function (e) {
      var that = this
      //改变index值，通过setData()方法重绘界面
      var currentdistrictIndex = e.detail.value
      var currentdistrictCode = e.currentTarget.dataset.councode[currentdistrictIndex] //当前区代表的Code,当做查询级联街道的parentid
      var district = e.currentTarget.dataset.name[currentdistrictIndex] //当前区代表的Code,当做查询级联街道的parentid
      that.setData({
        district: district,
        districtIndex: currentdistrictIndex,
      })
    }
})
